﻿@using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant
@using CleanArchitecture.Blazor.Application.Features.Identity.DTOs
@inherits MudComponentBase
@inject IStringLocalizer<Roles> L
@inject ITenantService TenantsService
<MudDialog>
    <DialogContent>
        <MudForm Model="Model" @ref="_form">
            <MudGrid>
                <MudItem sm="6" xs="12">
                    <MudSelect For="@(() => Model.TenantId)"
                               T="string"
                               Label="@L["Select Tenant"]"
                               Required="true"
                               @bind-Value="@Model.TenantId">
                        @foreach (var item in TenantsService.DataSource)
                        {
                            <MudSelectItem T="string" Value="@item.Id">@item.Name</MudSelectItem>
                        }
                    </MudSelect>
                </MudItem>
                <MudItem sm="6" xs="12">
                    <MudTextField Label="@L[Model.GetMemberDescription(x => x.Name)]" @bind-Value="Model.Name"
                                  Required="true"
                                  For="@(() => Model.Name)"
                                  RequiredError="@L["DefaultRole name is required!"]"
                                  MaxLength="50">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField Label="@L[Model.GetMemberDescription(x => x.Description)]"
                                  For="@(() => Model.Description)"
                                  MaxLength="255"
                                  @bind-Value="Model.Description">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">@ConstantString.Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit">@ConstantString.Ok</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = default!;

    [EditorRequired] [Parameter] public ApplicationRoleDto Model { get; set; } = default!;

    private MudForm? _form;

    private async Task Submit()
    {
        await _form!.Validate();
        if (_form!.IsValid)
        {
            MudDialog.Close(DialogResult.Ok(true));
        }
    }

    private void Cancel()
    {
        MudDialog.Cancel();
    }


}